package com.rhb.ca.web.online;

import com.rhb.ca.uzer.bean.Uzer;

import java.util.ArrayList;
import java.util.Hashtable;

public class OnLines{
    private static OnLines ols;
    
    private static Hashtable uzers;
    private static ArrayList onlines;
    private static Listener listener;

    public static synchronized OnLines getInstance(){
        if(ols == null)
            ols = new OnLines();
        return ols;
    }

    private OnLines(){
        this.uzers = new Hashtable();
        this.onlines = new ArrayList();
        try{
            this.listener = new Listener(onlines, uzers);
            this.listener.start();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void touchBy(OnLine ol){
        if(ols == null) getInstance();
        synchronized(onlines){
            int i = 0;
            for(int si = onlines.size(); i < si; i++){
                OnLine l = (OnLine)onlines.get(i);
                if(l != null && l.getSessionID().equals(ol.getSessionID())){
                    onlines.remove(i);
                    i--;si--;
                }
            }
            onlines.add(ol);
        }
    }

    public ArrayList getOnLines(String str[])throws Exception{
        return onlines;
    }

    public String[] getIPs()throws Exception{
    	ArrayList ips = new ArrayList();
        for(int i=0;onlines!=null&&i<onlines.size();i++){
        	OnLine ol = (OnLine)onlines.get(i);
        	if(ol==null||ol.getTouchIP()==null) continue;

			boolean has = false;
			for(int j=0;j<ips.size();j++){
				if(((String)ips.get(j)).equals(ol.getTouchIP()))
					has = true;
			}
	    	if(!has){
	    		ips.add(ol.getTouchIP());
	    	}
	    }
    	
        return (String[])ips.toArray(new String[ips.size()]);
    }

    public Uzer getUzer(String sessionID){
        Uzer uzer = null;
        synchronized(uzers){
            uzer = (Uzer)uzers.get(sessionID);
        }
        return uzer;
    }

    public OnLine getOnLine(String sessionID){
        OnLine ol = null;
        synchronized(onlines){
            int i = 0;
            for(int si = onlines.size(); i < si; i++){
                OnLine l = (OnLine)onlines.get(i);
                if(l != null && l.getSessionID().equals(sessionID)){
                    ol = new OnLine();
                    ol.init(l);
                    break;
                }
            }
        }
        return ol;
    }

    public void addOnLine(OnLine ol, Uzer uzer){
        if(uzer == null || ol == null || ol.getSessionID() == null || "".equals(ol.getSessionID()))
            return;
        synchronized(uzers){
            try{
                uzers.put(ol.getSessionID(), uzer);
                touchBy(ol);
            }catch(Exception e) { }
        }
    }

    public void removeOnLine(String sessionID){
        synchronized(uzers){
            try{
                uzers.remove(sessionID);
            }
            catch(Exception e) { }
        }
        synchronized(onlines){
            int i = 0;
            for(int si = onlines.size(); i < si; i++){
                OnLine l = (OnLine)onlines.get(i);
                if(l != null && l.getSessionID().equals(sessionID))
                    try{
                        onlines.remove(l);
                        i--;si--;
                    }
                    catch(Exception e) {}
            }
        }
    }
}
